<?php
/**
 * @author Jumper Swordwave
 * date: 2021/04/21
 */
namespace HyperfTest\Cases;

use PHPUnit\Framework\TestCase;
use Hyperf\Utils\ApplicationContext;
use Hyperf\Redis\Redis;
use App\Game\Service\{RoomService, UserService};

/**
 * @internal
 * @coverNothing
 */
class UserServiceTest extends TestCase
{
    protected function setUp() : void 
    {
        $this->redis = ApplicationContext::getContainer()->get(Redis::class);
        $this->service = ApplicationContext::getContainer()->get(RoomService::class);
        $this->service->clear();

        $this->userService = ApplicationContext::getContainer()->get(UserService::class);
        $this->userService->clear();
    }

    protected function tearDown() : void
    {
        $this->service->clear();
    }

    public function testNew()
    {
        $room = $this->service->newRoom();
        $this->assertNotNull($room);

        $room1 = unserialize($this->redis->get(sprintf(config('game.key.room_info'), $room->id)));

        $this->assertSame($room->id, $room1->id);
        $this->assertSame($room->status, $room1->status);
    }

    public function testUserJoin()
    {
        $user = $this->userService->newUser(rand(1, 99), ['nickname'=>'abcd']);
        $room = $this->service->newRoom();

        $result = $this->service->userJoin($room, $user);
        $this->assertTrue($result);
    }
}